<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" ref="filters" :size="size">
        <el-form-item prop="number">
          <el-input v-model="filters.number" placeholder="订单编号"></el-input>
        </el-form-item>
        <el-form-item prop="serveno">
          <el-input v-model="filters.serveno" placeholder="退款单编号"></el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="filters.username" placeholder="申请退款人"></el-input>
        </el-form-item>
        <el-form-item prop="shop_id">
          <el-select v-model="filters.shop_id" placeholder="请选择店铺">
            <el-option v-for="item in list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="status">
          <el-select v-model="filters.status" placeholder="请选择状态">
            <el-option
              v-for="item in listStatus"
              :key="item.id"
              :label="item.val"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
         <el-row>
        <el-form-item>
          <kt-button
            icon="fa fa-search"
            :label="$t('action.search')"
            type="primary"
            @click="findPage(null)"
          />
        </el-form-item>
        <el-form-item>
          <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
        </el-form-item>
       
        <el-form-item>
          <kt-button label="退货处理" type="primary" @click="setStaus()" />
        </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel" id="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      :showEditOperation="false"
      :showBatchDelete="false"
      @findPage="findPage"
      @selectionChange="selectionChange"
      @handleDelete="handleDelete"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      title="退款退货审核"
      width="30%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="服务编号" prop="serveno">
          <el-input
            v-model="dataForm.serveno"id
            :readonly="readonly"
            style="width: 214.5px"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="dataForm.status" style="width: 214.5px">
            <el-option
              v-for="item in listStatus"
              :key="item.id"
              :label="item.val"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import utils from "@/assets/js/utils.js";
export default {
  components: {
    PopupTreeInput,
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      readonly: true,
      list: [],
      listStatus: [],
      filters: {
        number: "",
        serveno: "",
        shop_id: "",
        username: "",
        status: "",
        t: "refund"
      },
      columns: [],
      checked: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      // 新增编辑界面数据
      dataForm: {
        shop_id: ""
      }
    };
  },

  methods: {
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPage(this.filters);
    },
    // 店铺和退货单状态初始化

    queryUserList() {
      var that = this;
      let param = {};
      param.t = "shop";
      this.utils.request.queryUserList(param, function(data) {
        that.list = data.data;
      });
      let params = {};
      params.t = "sysConfig";
      params.type = "refund_status";
      this.utils.request.queryUserList(params, function(data) {
        that.listStatus = data.data;
      });
    },

    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "退款退货";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },

    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "serveno", label: "服务编号", minWidth: 120 },
        { prop: "number", label: "订单编号", minWidth: 120 },
        { prop: "shopname", label: "所属店铺", minWidth: 120 },
        { prop: "username", label: "退款申请人", minWidth: 120 },
        { prop: "productname", label: "退货商品", minWidth: 150 },
        { prop: "pay_type", label: "付款方式", minWidth: 120 },
        { prop: "reciever", label: "收货人", minWidth: 120 },
        { prop: "reciever_tel", label: "收货电话", minWidth: 120 },
        { prop: "reciever_address", label: "收货人地址", minWidth: 120 },
        { prop: "statusname", label: "状态", minWidth: 120 },
        { prop: "create_time", label: "创建时间", minWidth: 120 }
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.findPage();
    },
    //选择框
    selectionChange: function(params) {
      this.checked = params.selections;
    },
    setStaus() {
      if (this.checked.length == 1) {
        let this_ = this;
        this_.dialogVisible = true;
        this_.dataForm = this_.checked[0];
      } else {
        this.$message({
          message: "审核只能选择一条数据",
          type: "error"
        });
      }
    },
    submitForm() {
      let this_ = this;
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataForm);
            params.t = "refund";

            this.utils.request.editUserInfo(params, function(data) {
              if (data.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });
                this_.dialogVisible = false;
                this_.editLoading = false;
              } else {
                this_.$message({
                  message: "操作失败, " + data.msg,
                  type: "error"
                });
              }
              this_.findPage();
            });
          });
        }
      });
    },
    // 批量删除
    handleDelete: function(data) {
      var ids = "";
      for (var i = 0; i < data.params.length; i++) {
        ids = ids + data.params[i].id + ",";
      }
      data.t = "refund";
      data.ids = ids;
      this.utils.request.deleteUserInfo(data, data.callback);
    }
  },
  mounted() {
    this.initColumns();
    this.queryUserList();
  }
};
</script>

<style scoped>
/deep/ .el-input__inner   {
  width: 200px !important;
}
</style>